import _objectSpread from "@babel/runtime/helpers/esm/objectSpread2";
import _createForOfIteratorHelper from "@babel/runtime/helpers/esm/createForOfIteratorHelper";
import { jsx as _jsx, Fragment as _Fragment, jsxs as _jsxs } from "react/jsx-runtime";
import { ConfigProvider as AntdConfigProvider } from 'antd-v4';
import zh_CN from "antd-v4/es/locale/zh_CN";
import React, { useContext, useEffect } from 'react';
import { SWRConfig, useSWRConfig } from 'swr';
import arEG from './locale/ar_EG';
import caES from './locale/ca_ES';
import deDE from './locale/de_DE';
import enGB from './locale/en_GB';
import enUS from './locale/en_US';
import esES from './locale/es_ES';
import faIR from './locale/fa_IR';
import frFR from './locale/fr_FR';
import hrHR from './locale/hr_HR';
import idID from './locale/id_ID';
import itIT from './locale/it_IT';
import jaJP from './locale/ja_JP';
import koKR from './locale/ko_KR';
import mnMN from './locale/mn_MN';
import msMY from './locale/ms_MY';
import plPL from './locale/pl_PL';
import ptBR from './locale/pt_BR';
import ruRU from './locale/ru_RU';
import srRS from './locale/sr_RS';
import trTR from './locale/tr_TR';
import viVN from './locale/vi_VN';
import zhCN from './locale/zh_CN';
import zhTW from './locale/zh_TW';

function get(source, path, defaultValue) {
  // a[3].b -> a.3.b
  var paths = path.replace(/\[(\d+)\]/g, '.$1').split('.');
  var result = source;
  var message = defaultValue; // eslint-disable-next-line no-restricted-syntax

  var _iterator = _createForOfIteratorHelper(paths),
      _step;

  try {
    for (_iterator.s(); !(_step = _iterator.n()).done;) {
      var p = _step.value;
      message = Object(result)[p];
      result = Object(result)[p];

      if (message === undefined) {
        return defaultValue;
      }
    }
  } catch (err) {
    _iterator.e(err);
  } finally {
    _iterator.f();
  }

  return message;
}
/**
 * 创建一个操作函数
 *
 * @param locale
 * @param localeMap
 */


var createIntl = function createIntl(locale, localeMap) {
  return {
    getMessage: function getMessage(id, defaultMessage) {
      return get(localeMap, id, defaultMessage) || defaultMessage;
    },
    locale: locale
  };
};

var mnMNIntl = createIntl('mn_MN', mnMN);
var arEGIntl = createIntl('ar_EG', arEG);
var zhCNIntl = createIntl('zh_CN', zhCN);
var enUSIntl = createIntl('en_US', enUS);
var enGBIntl = createIntl('en_GB', enGB);
var viVNIntl = createIntl('vi_VN', viVN);
var itITIntl = createIntl('it_IT', itIT);
var jaJPIntl = createIntl('ja_JP', jaJP);
var esESIntl = createIntl('es_ES', esES);
var caESIntl = createIntl('ca_ES', caES);
var ruRUIntl = createIntl('ru_RU', ruRU);
var srRSIntl = createIntl('sr_RS', srRS);
var msMYIntl = createIntl('ms_MY', msMY);
var zhTWIntl = createIntl('zh_TW', zhTW);
var frFRIntl = createIntl('fr_FR', frFR);
var ptBRIntl = createIntl('pt_BR', ptBR);
var koKRIntl = createIntl('ko_KR', koKR);
var idIDIntl = createIntl('id_ID', idID);
var deDEIntl = createIntl('de_DE', deDE);
var faIRIntl = createIntl('fa_IR', faIR);
var trTRIntl = createIntl('tr_TR', trTR);
var plPLIntl = createIntl('pl_PL', plPL);
var hrHRIntl = createIntl('hr_', hrHR);
var intlMap = {
  'mn-MN': mnMNIntl,
  'ar-EG': arEGIntl,
  'zh-CN': zhCNIntl,
  'en-US': enUSIntl,
  'en-GB': enGBIntl,
  'vi-VN': viVNIntl,
  'it-IT': itITIntl,
  'ja-JP': jaJPIntl,
  'es-ES': esESIntl,
  'ca-ES': caESIntl,
  'ru-RU': ruRUIntl,
  'sr-RS': srRSIntl,
  'ms-MY': msMYIntl,
  'zh-TW': zhTWIntl,
  'fr-FR': frFRIntl,
  'pt-BR': ptBRIntl,
  'ko-KR': koKRIntl,
  'id-ID': idIDIntl,
  'de-DE': deDEIntl,
  'fa-IR': faIRIntl,
  'tr-TR': trTRIntl,
  'pl-PL': plPLIntl,
  'hr-HR': hrHRIntl
};
var intlMapKeys = Object.keys(intlMap);
export { mnMNIntl, arEGIntl, enUSIntl, enGBIntl, zhCNIntl, viVNIntl, itITIntl, jaJPIntl, esESIntl, caESIntl, ruRUIntl, srRSIntl, msMYIntl, zhTWIntl, frFRIntl, ptBRIntl, koKRIntl, idIDIntl, deDEIntl, faIRIntl, trTRIntl, plPLIntl, hrHRIntl, intlMap, intlMapKeys };
export { ConfigConsumer, ConfigProvider, ConfigProviderWrap, createIntl };
var ConfigContext = /*#__PURE__*/React.createContext({
  intl: _objectSpread(_objectSpread({}, zhCNIntl), {}, {
    locale: 'default'
  }),
  valueTypeMap: {}
});
var ConfigConsumer = ConfigContext.Consumer,
    ConfigProvider = ConfigContext.Provider;
/**
 * 根据 antd 的 key 来找到的 locale 插件的 key
 *
 * @param localeKey
 */

var findIntlKeyByAntdLocaleKey = function findIntlKeyByAntdLocaleKey(localeKey) {
  if (!localeKey) {
    return 'zh-CN';
  }

  var localeName = localeKey.toLocaleLowerCase();
  return intlMapKeys.find(function (intlKey) {
    var LowerCaseKey = intlKey.toLocaleLowerCase();
    return LowerCaseKey.includes(localeName);
  });
};
/**
 * 组件解除挂载后清空一下 cache
 *
 * @returns
 */


var CacheClean = function CacheClean() {
  var _useSWRConfig = useSWRConfig(),
      cache = _useSWRConfig.cache;

  useEffect(function () {
    return function () {
      // is a map
      // @ts-ignore
      cache.clear();
    }; // eslint-disable-next-line react-hooks/exhaustive-deps
  }, []);
  return null;
};
/**
 * 如果没有配置 locale，这里组件会根据 antd 的 key 来自动选择
 *
 * @param param0
 */


var ConfigProviderWrap = function ConfigProviderWrap(_ref) {
  var _children = _ref.children,
      _ref$autoClearCache = _ref.autoClearCache,
      autoClearCache = _ref$autoClearCache === void 0 ? false : _ref$autoClearCache;

  var _useContext = useContext(AntdConfigProvider.ConfigContext),
      locale = _useContext.locale; // 如果 locale 不存在自动注入的 AntdConfigProvider


  var Provider = locale === undefined ? AntdConfigProvider : React.Fragment;

  var configProviderDom = _jsx(ConfigConsumer, {
    children: function children(value) {
      var _value$intl;

      var localeName = locale === null || locale === void 0 ? void 0 : locale.locale;
      var key = findIntlKeyByAntdLocaleKey(localeName); // antd 的 key 存在的时候以 antd 的为主

      var intl = localeName && ((_value$intl = value.intl) === null || _value$intl === void 0 ? void 0 : _value$intl.locale) === 'default' ? intlMap[key] : value.intl || intlMap[key]; // 自动注入 antd 的配置

      var configProvider = locale === undefined ? {
        locale: zh_CN
      } : {};
      return _jsx(Provider, _objectSpread(_objectSpread({}, configProvider), {}, {
        children: _jsx(ConfigProvider, {
          value: _objectSpread(_objectSpread({}, value), {}, {
            intl: intl || zhCNIntl
          }),
          children: _jsxs(_Fragment, {
            children: [autoClearCache && _jsx(CacheClean, {}), _children]
          })
        })
      }));
    }
  });

  if (!autoClearCache) return configProviderDom;
  return _jsx(SWRConfig, {
    value: {
      provider: function provider() {
        return new Map();
      }
    },
    children: configProviderDom
  });
};

export function useIntl() {
  var _useContext2 = useContext(AntdConfigProvider.ConfigContext),
      locale = _useContext2.locale;

  var _useContext3 = useContext(ConfigContext),
      intl = _useContext3.intl;

  if (intl && intl.locale !== 'default') {
    return intl;
  }

  if (locale === null || locale === void 0 ? void 0 : locale.locale) {
    return intlMap[findIntlKeyByAntdLocaleKey(locale.locale)];
  }

  return zhCNIntl;
}
export var ProProvider = ConfigContext;
export default ConfigContext;